# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=setuptools-scm
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.1.0
pkgrel=1
pkgdesc='Handles managing your python package versions in scm metadata (mingw-w64)'
url='https://github.com/pypa/setuptools_scm'
license=('MIT')
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
            )
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname/-/_}-${pkgver}.tar.gz"
        "0001-python-setuptools-scm-2.1.0-fix-path-error.patch")
sha256sums=('a767141fecdab1c0b3c8e4c788ac912d7c94a0d6c452d40777ba84f918316379'
            'ea9971a42f5f9debd8e37f587ed5c6d416316a835c0b94ff1da6e818dc26ef28')

prepare() {
  cd "${srcdir}/${_realname/-/_}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-python-setuptools-scm-2.1.0-fix-path-error.patch"
  
  cd "${srcdir}"
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH} | true
    cp -r "${_realname/-/_}-${pkgver}" "python${pver}-build-${CARCH}"
  done  
}

build() {
  cd "${srcdir}"
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

package_python3-setuptools-scm() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
  
  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
}

package_python2-setuptools-scm() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
}

package_mingw-w64-i686-python2-setuptools-scm() {
  package_python2-setuptools-scm
}

package_mingw-w64-i686-python3-setuptools-scm() {
  package_python3-setuptools-scm
}

package_mingw-w64-x86_64-python2-setuptools-scm() {
  package_python2-setuptools-scm
}

package_mingw-w64-x86_64-python3-setuptools-scm() {
  package_python3-setuptools-scm
}
